package com.jt.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.*;

import javax.sound.sampled.Port;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Configuration //标识我是一个配置类
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {

    /**
     * springBoot整合Redis分片
     * 实质：ShardedJedis对象，交给容器管理
     *
     */
    @Value("${redis.nodes}")
    private String nodes;       //node,node,node

    @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> nodeSet=new HashSet<>();
        String [] nodeArray=nodes.split(",");
        for (String node:nodeArray){
            String host=node.split(":")[0];
            int port=Integer.parseInt(node.split(":")[1]);
            nodeSet.add(new HostAndPort(host,port));
        }
        return new JedisCluster(nodeSet);
    }

    /*redis分片*/
    /*@Bean
    public ShardedJedis shardedJedis(){
        List<JedisShardInfo> shards=new ArrayList<>();
        String [] nodeArrays=nodes.split(",");
        for (String node:nodeArrays){
            String host=node.split(":")[0];
            int port=Integer.parseInt(node.split(":")[1]);
            //准备分片的节点信息
            JedisShardInfo info=new JedisShardInfo(host,port);
            shards.add(info);
        }



        return new ShardedJedis(shards);
    }*/







    /*@Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;

    @Bean
    public Jedis jedis(){
        return new Jedis(host,port);
    }*/
}
